<?php

namespace App\Http\Controllers;

use App\Http\Models\FightApply;
use Illuminate\Support\Facades\View;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Redirect;
use Illuminate\Support\Facades\Session;

class MessageController extends MemberController
{

    public function getCreate()
    {
        $today = date('Y-m-d', time());
        return View::make($this->modelName . "/create", compact('today'));
    }

    public function postCreate(Request $request)
    {
        $datas = $request->all();
        $modelName = 'App\Http\Models\\' . ucfirst($this->modelName);
        $model = new $modelName();
        $validate = Validator::make($datas, $model::$rule);
        if ($validate->fails()) {
            return View::make($this->modelName . '/create', compact('datas'))->withError($validate->messages());
        }
        if ($model->doSave($datas)) {
            return Redirect::to($this->modelName . '/create')->withSuccess("发布成功");
        } else {
            return redirect($this->modelName . '/create')->withErrors("发布失败");
        }
    }

    public function getList()
    {
        $modelName = 'App\Http\Models\\' . ucfirst($this->modelName);
        $model = new $modelName();
        $datas = $model->getList();
        return View::make($this->modelName . "/list", compact('datas'));
    }


    //申请加入
    public function doJoin(Request $request)
    {
        $datas = $request->all();
        //$datas['FACustomerID'] = Session::get('customer_id');
        $modelName = 'App\Http\Models\\' . ucfirst($this->modelName) . 'Apply';
        $model = new $modelName();
        if ($model->doSave($datas)) {
            return Redirect::to($this->modelName . '/list')->withSuccess("申请成功");
        } else {
            return Redirect::back();
        }
    }

    //我的约战
    public function getMy()
    {
        $modelName = 'App\Http\Models\\' . ucfirst($this->modelName);
        $model = new $modelName();
        $datas = $model->getMylist();
        return View::make($this->modelName . "/my", compact('datas'));
    }

    //约战编辑
    public function getEdit($data)
    {
        if ($data->FCustomerID != Session::get('customer_id')) {
            die('Access Denied!');
        }
        $applys = FightApply::getAoolys($data->FID); //获取约战申请信息
        return View::make($this->modelName . "/edit", compact('data', 'applys'));
    }

    public function doChange(Request $request)
    {
        $data = $request->all();
        $model = new FightApply();
        if ($model->doChange($data['FAID'], $data['FARes'])) {
            echo '1';
            die;
        } else {
            echo '0';
            die;
        }
    }

    public function getMyApply()
    {
        $modelName = 'App\Http\Models\\' . ucfirst($this->modelName) . 'Apply';
        $model = new $modelName();
        $datas = $model->myApply();
        return View::make($this->modelName."/myApply",compact('datas'));
    }

}
